<?php

namespace Home\Controller;

use Home\Service\ExpressService;
use Home\Service\WeixinAPIService;
use Think\Controller;

/**
 * 运单 Controller
 *
 * @author 李静波
 *        
 */
class ExpressController extends PSIBaseController {

	/**
	 * 寄件 - 主页面
	 */
	public function apply() {
		$ws = new WeixinAPIService();
		$openId = $ws->getOpenIdFromSession();
		if (! $openId) {
			redirect(__ROOT__ . "/Home");
			return;
		}
		
		$this->initVar();
		
		$this->assign("title", "寄件");
		
		$es = new ExpressService();
		$this->assign("myInfo", $es->getMyInfo($openId));
		
		$this->display();
	}

	/**
	 * 寄件POST
	 */
	public function applyPOST() {
		if (IS_POST) {
			$params = array(
					"startPlace" => I("post.startPlace"),
					"destPlace" => I("post.destPlace"),
					"recName" => I("post.recName"),
					"recAddress" => I("post.recAddress"),
					"recTel" => I("post.recTel"),
					"sendName" => I("post.sendName"),
					"sendAddress" => I("post.sendAddress"),
					"sendTel" => I("post.sendTel"),
					"cargo" => I("post.cargo"),
					"fragile" => I("post.fragile"),
					"claimValue" => I("post.claimValue"),
					"cargoMoney" => I("post.cargoMoney"),
					"memo" => I("post.memo")
			);
			
			$es = new ExpressService();
			
			$this->ajaxReturn($es->apply($params));
		}
	}

	/**
	 * 运单查询
	 */
	public function query() {
		$ws = new WeixinAPIService();
		$openId = $ws->getOpenIdFromSession();
		if (! $openId) {
			redirect(__ROOT__ . "/Home");
			return;
		}
		
		$this->initVar();
		
		$this->assign("title", "运单查询");
		
		if (IS_POST) {
			$this->assign("is_post", "1");
			
			$params = array(
					"ref" => I("post.ref")
			);
			
			$es = new ExpressService();
			$data = $es->query($params);
			if ($data) {
				$this->assign("has_data", "1");
				$this->assign("queryData", $data);
			} else {
				$this->assign("has_data", "0");
			}
		}
		
		$this->display();
	}

	public function queryProvince() {
		if (IS_POST) {
			$params = array();
			$es = new ExpressService();
			$this->ajaxReturn($es->queryProvince($params));
		}
	}

	public function queryCity() {
		if (IS_POST) {
			$params = array(
					"id" => I("post.id")
			);
			
			$es = new ExpressService();
			$this->ajaxReturn($es->queryCity($params));
		}
	}

	public function queryDistrict() {
		if (IS_POST) {
			$params = array(
					"id" => I("post.id")
			);
			
			$es = new ExpressService();
			$this->ajaxReturn($es->queryDistrict($params));
		}
	}
}